<?php 
#############################################################################
#  Module Mini Webboard for Opencart 1.4.8 Up  From Team SiamOpencart.com		  											   #
#  เว็บผู้พัฒนา www.siamopencart.com ,www.thaiopencart.com                                                                                 #
#  โดย Somsak2004 วันที่ 23 พฤศจิกายน 2553                                                                                                           #
#############################################################################
# โดยการสนับสนุนจาก                                                                                                                                            #
# Unitedsme.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์ จดโดเมน ระบบ Linux                                                                              #
# Net-LifeStyle.com : ผู้ให้บริการเช่าพื้นที่เว็บไซต์์ จดโดเมน ระบบ Linux																           #
# SiamWebThai.com : SEO ขั้นเทพ โปรโมทเว็บขั้นเซียน ออกแบบ พัฒนาเว็บไซต์ / ตามความต้องการ และถูกใจ Google 		   #
#############################################################################
class ControllerWebboardReadBoard extends Controller {
	private $error = array(); 
	private $quess = array(); 
	private $quess_new = array(); 
 	private $act;
 	private $id_board;
 	private $id_author;
 	private $user_name;
 	public function index() {

	    $this->language->load('webboard/read_board');

		$this->load->model('webboard/webboard');

    	$this->document->title = $this->language->get('heading_title')." : ".$this->model_webboard_webboard->getAskTitle($this->request->get['id']);
		


    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
           //เก็บข้อมูล	  
		    if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	
		   $this->id_board = $this->request->get['id'];
  
		   if (isset($_SESSION['user_id']) || $this->config->get('webboard_guest') =="1") {
		   //post โดยทีมงานร้าน
		   if (isset($_SESSION['user_id'])) {$this->id_author=$_SESSION['user_id'];}else{$this->id_author=0;}
	      $this->model_webboard_webboard->InsertNewQuess($this->id_board,$this->request->post['enquiry'],$this->id_author);
		   } else {
			 // post โดย user
		     $this->load->model('account/customer');
		     $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
		     $this->user_name=$customer_info['firstname'];
		     $this->model_webboard_webboard->InsertNewQuessByUser($this->id_board,$this->request->post['enquiry'],$this->user_name);
		   }
	      $this->redirect((HTTPS_SERVER . 'index.php?route=webboard/read_board/success&page='.$page.'&id='.$this->id_board));
   	}

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

    	$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['text_new'] = $this->language->get('text_new');
    	$this->data['text_detail'] = $this->language->get('text_detail');
    	$this->data['text_send'] = $this->language->get('text_send');
    	$this->data['text_send_alert'] = $this->language->get('text_send_alert');
		$this->data['entry_enquiry'] = $this->language->get('entry_enquiry');
		$this->data['entry_captcha'] = $this->language->get('entry_captcha');
		$this->data['text_author'] = $this->language->get('text_author');
		$this->data['text_quess'] = $this->language->get('text_quess');
		$this->data['text_time'] = $this->language->get('text_time');
		$this->data['text_quess'] = $this->language->get('text_quess');
		$this->data['text_reply'] = $this->language->get('text_reply');
		$this->data['text_nobody'] = $this->language->get('text_nobody');
		$this->data['alert'] = $this->language->get('alert');
	    $this->data['ask']=$this->model_webboard_webboard->getAskBoard($this->request->get['id']);
 		$this->data['webboard_guest'] = html_entity_decode($this->config->get('webboard_guest'));
   		
		$this->data['id'] = $this->request->get['id'];
		
		$this->data['register_user']=html_entity_decode($this->customer->isLogged());
        
		if (isset($_SESSION['user_id'])) {$this->data['admin']=$_SESSION['user_id'];}else{$this->data['admin']=null;}

		    if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	
		    if (isset($this->request->get['id'])) {
				$id = $this->request->get['id'];
			}
		    if (isset($this->request->get['quess_id'])) {
				$quess_id = $this->request->get['quess_id'];
			}
   			if (isset($this->request->get['act'])) {
				$this->act = $this->request->get['act'];
			} 
			if ($this->act=="delete") {
			    $this->model_webboard_webboard->DeleteQuess($quess_id);		 
			    $redirect=$this->redirect((HTTPS_SERVER . 'index.php?route=webboard/read_board&page='.$page.'&id='.$id)); 
		   }

		
		if (isset($this->error['enquiry'])) {
			$this->data['error_enquiry'] = $this->error['enquiry'];
		} else {
			$this->data['error_enquiry'] = '';
		}		
		
 		if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}	



    	$this->data['button_continue'] = $this->language->get('button_continue');
    
    	
		if (isset($this->request->post['title'])) {
			$this->data['title'] = $this->request->post['title'];
		} else {
			$this->data['title'] = '';
		}

	
		if (isset($this->request->post['enquiry'])) {
			$this->data['enquiry'] = $this->request->post['enquiry'];
		} else {
			$this->data['enquiry'] = '';
		}
		
		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}		
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/webboard/read_board.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/webboard/read_board.tpl';
		} else {
			$this->template = 'default/template/webboard/read_board.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);

   			if (isset($this->request->get['id'])) {
				$id = $this->request->get['id'];
			} 
   			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	

				$pagination = new Pagination();
				$pagination->total =  $this->model_webboard_webboard->getQuessBoardAll($id);
				$pagination->page = $page;
				$pagination->limit = html_entity_decode($this->config->get('webboard_num_read'));  //กำหนดจำนวนแสดงต่อหน้า
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = HTTP_SERVER."index.php?route=webboard/read_board&id=".$id."&page=".$page;
				$pagination->url =HTTP_SERVER."index.php?route=webboard/read_board&id=".$id."&page=".$page;
 				$this->data['pagination'] = $pagination->render();
				$this->data['quess']= $this->model_webboard_webboard->getQuessBoard($id,$page,$pagination->limit);
			    $this->data['action'] = (HTTP_SERVER . 'index.php?route=webboard/read_board&id='.$id.'&page='.$page);
			    $this->data['page'] = $page;
			 //เพิ่มจำนวนการอ่าน
			 $this->model_webboard_webboard->AddRead($this->request->get['id']);
 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
  	}

  	public function success() {
		  $id = $this->request->get['id'];
		  if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	
		$this->language->load('webboard/read_board');

		$this->document->title = $this->language->get('heading_title'); 

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => (HTTP_SERVER . 'index.php?route=webboard/read_board'),
        	'text'      => $this->language->get('heading_title'),
        	'separator' => $this->language->get('text_separator')
      	);	
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_message'] = $this->language->get('text_message');

    	$this->data['button_continue'] = $this->language->get('button_continue');

    	$this->data['continue'] = (HTTP_SERVER . 'index.php?route=webboard/read_board&page='.$page.'&id='.$id);

		$this->load->model('webboard/webboard');

 			if (isset($this->request->get['id'])) {
					$this->data['ask']=$this->model_webboard_webboard->getAskBoard($this->request->get['id']);
		}			
	

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		} else {
			$this->template = 'default/template/common/success.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);


 		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); 
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
  	private function validate() {
 
		if ((strlen(utf8_decode($this->request->post['enquiry'])) < 10) || (strlen(utf8_decode($this->request->post['enquiry'])) > 1000)) {
      		$this->error['enquiry'] = $this->language->get('error_enquiry');
    	}

    	if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		$this->error['captcha'] = $this->language->get('error_captcha');
    	}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  	  
  	}
}

?>
